<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include('lib/include.php');
//session_start();
if (!isset($_SESSION['admin_is_login']))
{
	redirect('admin_login.php');
}
else
{
	if ($_SESSION['admin_is_login'] == 'no')
	{
		redirect('admin_login.php');
	}
}

$menu_out = getMenuHtml();

$product_category_id = -1;
$product_sub_category_id = -1;
$product_brand_id = -1;
$product_name = '';
$product_desc = '';
$product_price = '';
$product_discount = '';
$product_active = 1;
$product_img = '';
$product_img_url = '';
$product_id = '';
$action = '';
$dybtn ='';
$action_status = '';
$drop_disabled = '';
$product_is_home_page = '';
$product_is_clearance = '';
//pid=4&bid=1&sid=5&cid=5&name=ACER%20A570&img=uploaded_img/ina.jpg&desc=acer%20cell%20phone&price=100.00&price//_d=11.00&active=1

if (isset($_GET['action']))
{
	$action = $_GET['action'];
	if ($action == 'Add New')
	{
		$_SESSION['action'] = 'Add New';
	}
	if ($action == 'Update')
	{
		$_SESSION['action'] = 'Update';
		$dybtn = '<input type="button" id="btn_delete" name="btn_delete" value="Delete" onclick="product_delete();"';
		$drop_disabled = 'disabled="disabled"';
	}
}

if (isset($_POST['action_status']))
{
	$action_status = $_POST['action_status'];
}


if (isset($_POST['product_id']))
{
	$product_id = $_POST['product_id'];
}
else if (isset($_GET['pid']))
{
	$product_id = $_GET['pid'];
}

if (isset($_POST['product_category']))
{
	$product_category_id = $_POST['product_category'];
}
else if (isset($_GET['cid']))
{
	$product_category_id = $_GET['cid'];
}

if (isset($_POST['product_sub_category']))
{
	$product_sub_category_id = $_POST['product_sub_category'];
}
else if (isset($_GET['sid']))
{
	$product_sub_category_id = $_GET['sid'];
}

if (isset($_POST['product_brand']))
{
	$product_brand_id = $_POST['product_brand'];
}
else if (isset($_GET['bid']))
{
	$product_brand_id = $_GET['bid'];
}

if (isset($_POST['product_name']))
{
	$product_name = $_POST['product_name'];
}
else if (isset($_GET['name']))
{
	$product_name = $_GET['name'];
}

if (isset($_POST['product_description']))
{
	$product_desc = $_POST['product_description'];
}
else if (isset($_GET['desc']))
{
	$product_desc = $_GET['desc'];	
}

if (isset($_POST['product_price']))
{
	$product_price = $_POST['product_price'];
}
else if (isset($_GET['price']))
{
	$product_price = $_GET['price'];
}

if (isset($_POST['product_discount_price']))
{
	$product_discount = $_POST['product_discount_price'];
}
else if (isset($_GET['price_d']))
{
	$product_discount = $_GET['price_d'];
}

if (isset($_POST['chk_product_active']))
{
	$product_active = $_POST['chk_product_active'];
}
else if (isset($_GET['active']))
{
	$product_active = $_GET['active'];;
}

if ($product_active == 1 || $product_active == 'on') 
{
	$product_active = 'checked="true"';
}
else
{
	$product_active = "";
}

// is home page
if (isset($_POST['chk_product_is_home_page']))
{
	$product_is_home_page = $_POST['chk_product_is_home_page'];
}
else if (isset($_GET['home']))
{
	$product_is_home_page = $_GET['home'];;
}

if ($product_is_home_page == 1 || $product_is_home_page == 'on') 
{
	$product_is_home_page = 'checked="true"';
}
else
{
	$product_is_home_page = "";
}

// is clearance
if (isset($_POST['chk_product_is_clearance']))
{
	$product_is_clearance = $_POST['chk_product_is_clearance'];
}
else if (isset($_GET['clearance']))
{
	$product_is_clearance = $_GET['clearance'];;
}

if ($product_is_clearance == 1 || $product_is_clearance == 'on') 
{
	$product_is_clearance = 'checked="true"';
}
else
{
	$product_is_clearance = "";
}

if (isset($_GET['img']))
{
	$product_img = $_GET['img'];
	$product_img_url = '<img src="'.$product_img.'" width="100" height="100"></a>';
}
else if (isset($_POST['product_img']))
{
	$product_img = $_POST['product_img'];
}


if ($action_status == 'delete')
{
	if (count($product_id) != 0)
	{
		unlink($product_img);
		deleteProdByID($product_id);
		header('location: admin_product_list.php');
	}
}
else
{
	// insert/upload/update	
	$dest = "uploaded_img/";
	if (isset($_FILES['product_img_input']['name']))
	{
		$prodInfo = popProdInfo($_POST);
		if ($_FILES['product_img_input']['name'] != '')
		{
			$arr = getdate();
			$targetName = $dest.$arr[0].'_'.$_FILES['product_img_input']['name'];
			$tmpName = $_FILES['product_img_input']['tmp_name'];
			
			$uploadPath = fileUpload($tmpName,$targetName);							
			$prodInfo['product_img_input'] = $uploadPath;
			
			// id is '' -> insert
			if ($_POST['action_status'] == 'insert')
			{
				
				$res = insertProd($prodInfo);
				if ($res)
				{
					print('<script>alert("Product was inserted successfully!");</script>');
				}
				else 
				{
					// roll back uploaded file
					unlink($uploadPath);
					die('Product insertion failed.');
				}
			}
			
			if ($_POST['action_status'] == 'update')
			{
				$res = updateProd($prodInfo);
				if ($res)
				{
					if ($_POST['product_img'] != '')
					{
						unlink($_POST['product_img']);
					}
					print('<script>alert("Product was updated successfully!");</script>');
					header('location: admin_product_list.php');
				}
				else 
				{
					// roll back uploaded file
					die('Product update failed.');
				}
			}
		}
		else
		{
			if ($_POST['action_status'] == 'insert')
			{			
				$res = insertProd($prodInfo);
				if ($res){
					print('<script>alert("Product was inserted successfully!");</script>');
				}
				else {
					// roll back uploaded file
					//unlink($uploadPath);
					die('Product insertion failed.');
				}
			}
			
			if ($_POST['action_status'] == 'update')
			{
				$res = updateProd($prodInfo);
				if ($res)
				{
					print('<script>alert("Product was updated successfully!");</script>');
					header('location: admin_product_list.php');
				}
				else 
				{
					// roll back uploaded file
					die('Product update failed.');
				}
			}	
		}	
	}

}

function fileRename($name)
{
	
}
// return -2 means file duplication
// return -1 means upload fail
function fileUpload($tmp,$dest)
{
	if (file_exists($dest))
	{
		return -2;
	}
	
	if (move_uploaded_file($tmp,$dest))
	{
		return $dest;
	}
	else 
	{
		return -1;
	}
}

/*
$brandInfoArray = getBrands();
$brand_options = '';										
if (isset($brandInfoArray))
{
	for ($n = 0; $n < count($brandInfoArray); $n++)
	{
		$brandItem = $brandInfoArray[$n];
		$brand_options .= '<option value="'.$brandItem['id'].'">'.$brandItem['brand_name'].'</option>'; 
	}
}	
*/

$categoryInfoArray = getCategories();
$category_options = '';
if (isset($categoryInfoArray))
{
	for ($n = 0; $n < count($categoryInfoArray); $n++)
	{
		$categoryItem = $categoryInfoArray[$n];
		if ($product_category_id == $categoryItem['id'])
		{
			$category_options .= '<option value="'.$categoryItem['id'].'" selected="true">'.$categoryItem['category_name'].'</option>'; 
		}
		else
		{
			$category_options .= '<option value="'.$categoryItem['id'].'">'.$categoryItem['category_name'].'</option>'; 
		}
	}	
}

$sub_categoryInfoArray = getSubCategoriesByCategoryID($product_category_id);

$sub_category_options = '';
if (isset($sub_categoryInfoArray))
{
	for ($n = 0; $n < count($sub_categoryInfoArray); $n++)
	{
		$sub_categoryItem = $sub_categoryInfoArray[$n];
		if ($product_sub_category_id == $sub_categoryItem['id'])
		{
			$sub_category_options .= '<option value="'.$sub_categoryItem['id'].'" selected="true">'.$sub_categoryItem['sub_category_name'].'</option>'; 		
		}
		else
		{
			$sub_category_options .= '<option value="'.$sub_categoryItem['id'].'">'.$sub_categoryItem['sub_category_name'].'</option>'; 
		}
		
	}	
}

$brandInfoArray = getBrandsBySubCategoryID($product_sub_category_id);
$brand_options = "";
{
	for ($n = 0; $n < count($brandInfoArray); $n++)
	{
		$brandItem = $brandInfoArray[$n];
		if ($product_brand_id == $brandItem['id'])
		{
			$brand_options .= '<option value="'.$brandItem['id'].'" selected="true">'.$brandItem['brand_name'].'</option>'; 		
		}
		else
		{
			$brand_options .= '<option value="'.$brandItem['id'].'">'.$brandItem['brand_name'].'</option>'; 
		}
		
	}	
}


?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>888 Accessories</title>

<meta name="keywords" content="" />
<meta name="description" content="" />

<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" href="css/css.css" />

<script type="text/javascript" src="scripts/jquery.min.js.1.4.2.js"></script>
<script type="text/javascript" src="scripts/admin.js"></script>
<script type="text/javascript" src="ddaccordion.js"></script>
<script type="text/javascript">

ddaccordion.init({
	headerclass: "headerbar", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: "mouseover", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
	defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: true, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", "selected"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "normal", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})

</script>

<style type="text/css">
<!--
.style1 {color: #FF0000}
.style2 {
	font-size: 12px;
	font-family: Arial, Helvetica, sans-serif;
	font-weight: bold;
}
-->
</style>

<style type="text/css">

.urbangreymenu{
	width: 225px; /*width of menu*/
}

.urbangreymenu .headerbar{
	font: bold 13px Verdana;
	color: white;
	background: #606060 url(arrowstop.gif) no-repeat 8px 6px; /*last 2 values are the x and y coordinates of bullet image*/
	margin-bottom: 0; /*bottom spacing between header and rest of content*/
	text-transform: uppercase;
	padding: 7px 0 7px 31px; /*31px is left indentation of header text*/
}

.urbangreymenu .headerbar a{
	text-decoration: none;
	color: white;
	display: block;
}

.urbangreymenu ul{
	list-style-type: none;
	margin: 0;
	padding: 0;
	margin-bottom: 0; /*bottom spacing between each UL and rest of content*/
}

.urbangreymenu ul li{
	padding-bottom: 2px; /*bottom spacing between menu items*/
}

.urbangreymenu ul li a{
	font: normal 12px Arial;
	color: black;
	background: #E9E9E9;
	display: block;
	padding: 5px 0;
	line-height: 17px;
	padding-left: 8px; /*link text is indented 8px*/
	text-decoration: none;
}

.urbangreymenu ul li a:visited{
	color: black;
}

.urbangreymenu ul li a:hover{ /*hover state CSS*/
	color: white;
	background: black;
}

</style>

</head>

<body>

<div id="main">
	<div id="main1">
		<div class="logo2"><img src="images/banner.jpg" width="1000" height="84" />
	</div>
</div>

<div id="main2">

	<div id="body1">

<table width="1000" border="0" cellpadding="0" cellspacing="10">

<!--DWLayoutTable-->

<tr>

<td width="234" rowspan="2" valign="top">

<div class="testimonials">

<img src="images/logo.jpg" width="225" height="300" /></div>

<br />

<div class="testimonials">
  
<div align="center">
<span class="body2text2">Search</span>
<label>
<input name="textfield" type="text" id="textfield" maxlength="5" />
</label>

<span class="style2">GO</span></div>
</div>

<br />

<div class="leftnav"></div>

<div class="urbangreymenu">
<?php  echo $menu_out; ?>
<!--
<h3 class="headerbar"><a href="#">Cell Phone Parts</a></h3>
<ul class="submenu">
<li><a href="#">LCDS</a></li>
<li><a href="#">DIGITIZERS</a></li>
<li><a href="#">BATTERY DOORS</a></li>
<li><a href="#">BATTERIES</a></li>
<li><a href="#">FLEX</a></li>
<li><a href="#">HOUSING</a></li>
<li><a href="#">CHARGING PORTS</a></li>
<li><a href="#">BUTTON</a></li>
<li><a href="#">SIM</a></li>
<li><a href="#">CAMERAS</a></li>
<li><a href="#">LENS</a></li>
<li><a href="#">SPEAKERS</a></li>
<li><a href="#">KEYPADS</a></li>	
<li><a href="#">MID PLATE</a></li>
<li><a href="#">GEVEY</a></li>
<li><a href="#">TOOLS</a></li>	
<li><a href="#">MISC</a></li>
</ul>

<h3 class="headerbar"><a href="#">Accessories</a></h3>
<ul class="submenu">
<li><a href="#" >CAR CHARGERS</a></li>
<li><a href="#">HOME CHARGERS</a></li>
<li><a href="#">SCREEN PROTECTORS</a></li>
<li><a href="#">POUCHES</a></li>
<li><a href="#">CAR HOLDERS</a></li>
<li><a href="#">HEADSETS</a></li>
<li><a href="#">CABLES</a></li>
<li><a href="#">FM TRANSMITTERS</a></li>
<li><a href="#">TPU</a></li>
<li><a href="#">CASES</a></li>
<li><a href="#">ARM BANDS</a></li>
<li><a href="#">SLEEVES</a></li>
<li><a href="#">BUMPERS</a></li>
<li><a href="#">SNAP ON</a></li>
<li><a href="#">MEMORY CARDS</a></li>
<li><a href="#">SPEAKERS</a></li>
<li><a href="#">MISC</a></li>		
</ul>

<h3 class="headerbar"><a href="#">Cell Phones</a></h3>
<ul class="submenu">
<li><a href="#">ACER</a></li>
<li><a href="#">ALIENWARE</a></li>
<li><a href="#">APPLE</a></li>
<li><a href="#">CASIO</a></li>
<li><a href="#">HTC</a></li>
<li><a href="#">INQ</a></li>
<li><a href="#">LG</a></li>
<li><a href="#">MOTOROLA</a></li>
<li><a href="#">PANTECH</a></li>
<li><a href="#">RIM</a></li>
<li><a href="#">SAMSUNG</a></li>
<li><a href="#">SONY ERICSSON</a></li>
<li><a href="#">TRION</a></li>
</ul>

<h3 class="headerbar"><a href="#">LED Signs</a></h3>
<ul class="submenu">	
</ul>
<br />
-->
</div></td>
<td width="742" height="17" align="center" valign="top" bgcolor="#F4F4F4">
</td>
</tr>

<tr>

<td rowspan="2" valign="top" class="body2"><table width="89%" border="0" cellpadding="0" cellspacing="10">
  <!--DWLayoutTable-->
  <tr>
    <td width="720" height="41" valign="top" class="body2text"><h1>Administration</h1></td>
  </tr>

  <tr>
    <td height="25" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#cccccc">
      <tr>
        <td class="adminTitle">Product Administration</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="25" valign="top">
      <form id="product_form" name="product_name" enctype="multipart/form-data" action="admin_product.php" method="post"/>
      <input type="hidden" id="action_status" name="action_status" value=""/>  
      <table>
          <tr>
            <th class="adminItemHeader">Items</th>
            <th colspan="3" class="adminInfoHeader">Information</th>
          </tr>
          
          <tr>
            <td class="adminItem"><strong><span class="style1">*</span>Category</strong>
                <input type="hidden" id="product_category" name="product_category" value="<?php echo $product_category_id; ?>"/>            </td>
            <td class="adminInfo">
            	<span>
              		<select id="product_category_select" name="product_category_select" class="" onchange="product_category_select_onchange();" <?php echo $drop_disabled;?>>
                		<option value="-1">Select One</option>
                		<?php echo ($category_options); ?>
              		</select>
            </span> </td>
          </tr> 
          
          <tr>
            <td class="adminItem"><strong><span class="style1">*</span>Sub Category</strong>
                <input type="hidden" id="product_sub_category" name="product_sub_category" value="<?php echo $product_sub_category_id; ?>"/>            </td>
            <td class="adminInfo"><span>
              <select id="product_sub_category_select" name="product_sub_category_select" class="" onchange="product_sub_category_select_onchange();" <?php echo $drop_disabled;?>>
                <option value="-1">Select One</option>
                <?php echo ($sub_category_options); ?>
              </select>
            </span> </td>
          </tr> 
          
          
          <tr>
            <td class="adminItem"><strong><span class="style1">*</span>Brand</strong>
                <input type="hidden" id="product_brand" name="product_brand" value="<?php echo $product_brand_id; ?>"/>            </td>
            <td class="adminInfo"><span>
              <select id="product_brand_select" name="product_brand_select" class="" onchange="product_brand_select_onchange();" <?php echo $drop_disabled;?>>
                <option value="-1">Select One</option>
                <?php echo ($brand_options); ?>
              </select>
            </span></td>
          </tr> 
                   
          <tr>
            <td class="adminItem"><strong><span class="style1">*</span>Product Name</strong>
                <input type="hidden" id="product_id" name="product_id" value="<?php echo($product_id); ?>"/>            </td>
            <td class="adminInfo">
            	<input type="text" id="product_name" name="product_name" value="<?php echo($product_name);?>" class="adminInfoInput"/>            </td>
          </tr>

          <tr>
            <td class="adminItem"><strong><span class="style1">*</span>Product Image</strong>
                <input type="hidden" id="product_img" name="product_img" value="<?php echo($product_img);?>"/>            </td>
            <td class="adminInfo"><input type="file" id="product_img_input" name="product_img_input" value=""/>            </td>
          </tr>
          <tr>
            <td class="adminItem"><strong>Description</strong> </td>
            <td class="adminInfo">
            <input type="text" id="product_description" name="product_description" value="<?php echo($product_desc);?>" class="adminInfoInput"/>            </td>
          </tr>
          <tr >
            <td class="adminItem"><strong><span class="style1">*</span>Price</strong> </td>
            <td class="adminInfo">
            <input type="text" id="product_price" name="product_price" value="<?php echo($product_price);?>" class="adminInfoInput"/>            </td>
          </tr>
          <tr >
            <td class="adminItem"><strong>Discount Price</strong> </td>
            <td class="adminInfo">
            <input type="text" id="product_discount_price" name="product_discount_price" value="<?php echo($product_discount);?>" class="adminInfoInput"/>            </td>
          </tr>
          <tr >
            <td class="adminItem"><strong><span class="style1">*</span>Active</strong>
                <input type="hidden" id="product_active" name="product_active" value=""/>            </td>
            <td class="adminInfo"><input type="checkbox" id="chk_product_active" name="chk_product_active" <?php echo ($product_active);?>/></td>
          </tr>
          <tr >
            <td class="adminItem"><strong><span class="style1"></span>Is at home page</strong>
                <input type="hidden" id="product_is_home_page" name="product_is_home_page" value=""/>            </td>
            <td class="adminInfo"><input type="checkbox" id="chk_product_is_home_page" name="chk_product_is_home_page" <?php echo ($product_is_home_page);?>/></td>
          </tr>
          <tr >
            <td class="adminItem"><strong><span class="style1"></span>Is clearance</strong>
                <input type="hidden" id="product_is_clearance" name="product_is_clearance" value=""/>            </td>
            <td class="adminInfo"><input type="checkbox" id="chk_product_is_clearance" name="chk_product_is_clearance" <?php echo ($product_is_clearance);?>/></td>
          </tr>
          
      </table>
      	<?php echo($product_img_url);?>
      </form>
    </td>
    
  </tr>
  <tr>
    <td height="25" valign="top"><table width="auto" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="44" class="adminControlsLine">
        	<input type="button" id="btn_product_save" name="btn_product_save" value="<?php echo $_SESSION['action'];?>" onclick="product_save();"/>        
         </td>
        <td width="33" class="adminControlsLine"></td>
        <td width="46" class="adminControlsLine">
  			<?php echo $dybtn; ?>
        </td>
        <td width="33" class="adminControlsLine"></td>
        <td width="200" nowrap="nowrap" class="adminControlsLine">&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="25" valign="top">
    	<table width="auto" border="0" cellpadding="0" cellspacing="0" bgcolor="#cccccc">
      		<tr>
        		<td><input type="button" id="back_admin" name="back_admin" value="Back to Admin Page" onclick="back_admin();"/></td>
                <td width="280px"></td>
      		</tr>
    	</table>
    </td>
  </tr>
</table></td>
</tr>
</table>


</div>

</div>

<div id="bottom"> 

<div class="bottom2">

<a href="requiring_a_mobile_locksmith.html" title="Requiring a Mobile Locksmith">HOME</a>&nbsp;&nbsp;|&nbsp;&nbsp;
<a href="ottawa_locksmith.html" title="Ottawa Locksmith">ABOUT US</a> &nbsp;&nbsp;|&nbsp;&nbsp;<a href="keys_to_look_for_when_hiring_a_locksmith.html" title="Keys to look for when hiring a locksmith">CONTACT US</a><br /> 

<em><strong>&copy; 888 Accessories. All Rights Reserved</strong></em>

</div>

</div>

</div>

</body>

</html>